﻿namespace Jhong.FunMVCBlog.Admin.Controllers
{
    using Jhong.FunMVC;
    using Jhong.FunMVCBlog.Core.Data;
    using Jhong.FunMVCBlog.Core.Entity;
    using Jhong.FunMVCBlog.Core.Infrastructure;
    using System;

    public class TagController : SecurityController
    {
        public ActionResult List()
        {
            return View();
        }

        private TagData _tagData = new TagData();

        public ActionResult GetData(int pageIndex, int itemsPerPage)
        {
            var data = this._tagData.GetList(pageIndex, itemsPerPage);
            var page = data.GetPageHelper();
            return AjaxResult(true, data: new { Items = data.Items, Page = page.GetPageHtml() });
        }

        public ActionResult EditTag(string id)
        {
            if (string.IsNullOrWhiteSpace(id))
            {
                ViewData["model"] = new Tag();
                return View();
            }
            ViewData["model"] = this._tagData.GetByID(Guid.Parse(id));
            return View();
        }

        [HttpPost]
        public ActionResult EditTag(Tag model)
        {
            model.LastModifyTime = DateTime.Now;
            bool res;
            if (model.ID == Guid.Empty)
            {
                model.CreateTime = DateTime.Now;
                model.ID = Guid.NewGuid();
                res = this._tagData.Insert(model);
            }
            else
            {
                res = this._tagData.Update(model);
            }
            return AjaxResult(res, res ? "保存成功" : "保存失败");
        }

        public ActionResult DeleteTag(Guid id)
        {
            var res = this._tagData.DeleteByID(id);
            return AjaxResult(res, res ? "删除成功" : "删除失败");
        }
    }
}